package gen;

import gen.model.IDatatype;
import gen.model.ModelAttribute;
import gen.model.ModelClass;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class GenInterfaceHelper {
	static void generateInterface(ModelClass modelClass, Map<String, IDatatype> objects) throws IOException {
		String package_ = "ar.com.coonocer.joy.mdg.xmi." + modelClass.getLabel();
		//
		File f = new File(GenerateModelClasses.ROOT + "/" + package_.replace('.', '/') + "/" + modelClass.getName() + ".java");
		f.getParentFile().mkdirs();
		FileWriter fw = new FileWriter(f);
		//
		fw.write("package " + package_ + ";\r\n");
		//
		fw.write("import ar.com.coonocer.joy.mdg.xmi.core.Class;\r\n");
		fw.write("import ar.com.coonocer.joy.mdg.xmi.model.Package;\r\n");
		fw.write("import ar.com.coonocer.joy.mdg.xmi.behavior.Object;\r\n");
		fw.write("import ar.com.coonocer.joy.mdg.xmi.behavior.Exception;\r\n");
		fw.write("\r\n");
		fw.write("import ar.com.coonocer.joy.mdg.xmi.activity.*;\r\n");
		fw.write("import ar.com.coonocer.joy.mdg.xmi.behavior.*;\r\n");
		fw.write("import ar.com.coonocer.joy.mdg.xmi.collab.*;\r\n");
		fw.write("import ar.com.coonocer.joy.mdg.xmi.core.*;\r\n");
		fw.write("import ar.com.coonocer.joy.mdg.xmi.datatype.*;\r\n");
		fw.write("import ar.com.coonocer.joy.mdg.xmi.usecase.*;\r\n");
		fw.write("import ar.com.coonocer.joy.mdg.xmi.model.*;\r\n");
		fw.write("import ar.com.coonocer.joy.mdg.xmi.state.*;\r\n");
		fw.write("import java.util.List;\r\n");
		fw.write("import java.util.ArrayList;\r\n");
		//
		fw.write("public interface " + modelClass.getName() + " ");
		if (!modelClass.supertypes.isEmpty()) {
			fw.write("extends ");
			//
			boolean first = true;
			for (String supertypeId : modelClass.supertypes) {
				if (first) {
					first = false;
				} else {
					fw.write(",");
				}
				fw.write(GenerateModelClasses.getTypeName(supertypeId, objects));
			}
		}
		fw.write("{");
		fw.write("\r\n");
		//
		generateInterfaceMethods(fw, modelClass, objects);
		//
		fw.write("}");
		fw.close();
	}

	private static void generateInterfaceMethods(FileWriter fw, ModelClass modelClass, Map<String, IDatatype> objects) throws IOException {
		List<ModelAttribute> atts = modelClass.attributes;
		for (ModelAttribute att : atts) {
			//
			//
			//String singulartype = GenerateModelClasses.getTypeName(att.type, objects);
			String type = GenerateModelClasses.generateDataTypeForMany(att, objects);
			//
			// getter
			fw.write("	public " + type + " get" + GenerateModelClasses.upperInitial(att.name) + "();\r\n");
			// setter
			fw.write("	public void set" + GenerateModelClasses.upperInitial(att.name) + "(" + type + " " + GenerateModelClasses.getVariableName(att.name) + ");\r\n");
			//
		}
	}

}
